package com.dropesoft.ui;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dropesoft.biz.TrainingDbAdapter;
import com.dropesoft.entities.Training;

import dropesoft.com.R;

public class DettaglioAllenamento extends Activity{
	private TrainingDbAdapter _ga = null;
	private long _idAllenamento;
	
	public long getIdAllenamento() {
		return _idAllenamento;
	}

	public void setIdAllenamento(long idAllenamento) {
		this._idAllenamento = idAllenamento;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.training_detail);
		
		_ga = new TrainingDbAdapter(this);
		_ga.open();
		
		// Recupera l'id dell'allenamento
		Bundle extra = getIntent().getExtras();
		if(extra != null)
		{
			long id = extra.getLong("ida");
			setIdAllenamento(id);
			caricaDatiAllenamento();
		}
	}
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		_ga.close();
	}
	
	private void caricaDatiAllenamento()
	{
		Training allenamento = _ga.getTraining(getIdAllenamento());
		//SimpleDateFormat df = new SimpleDateFormat("EEE dd/MM/yy");
		
		//((TextView)findViewById(R.id.txtData)).setText(df.format(allenamento.getData()));
		
		int ore =  (int)allenamento.getTempo() / 3600;
		int minuti = (int)((allenamento.getTempo() % 3600)/60);
		int secondi = (int)((allenamento.getTempo() % 3600)%60);
		
		((TextView)findViewById(R.id.txtTempoOra)).setText(String.valueOf(ore) + " h");
		((TextView)findViewById(R.id.txtTempoMinuti)).setText(String.valueOf(minuti) + " m");
		((TextView)findViewById(R.id.txtTempoSecondi)).setText(String.valueOf(secondi) + " s");
		((TextView)findViewById(R.id.txtDistanza)).setText(String.valueOf(allenamento.getDistanza()));
		
		float passoMed = allenamento.getPassoMed();
		if (passoMed > 0)
		{
			String passoStr =  String.valueOf(passoMed);
			((TextView)findViewById(R.id.txtPassoMedio)).setText(passoStr.substring(0, passoStr.indexOf(".")) + " : " + passoStr.substring(passoStr.indexOf(".")+1));
		 
		}
		((TextView)findViewById(R.id.txtVelocitaMedia)).setText(String.valueOf(allenamento.getVelocitaMed()));
		((TextView)findViewById(R.id.txtBpm)).setText(String.valueOf(allenamento.getBpmMed()));
		((TextView)findViewById(R.id.txtCalorie)).setText(String.valueOf(allenamento.getCalorie()));
		((TextView)findViewById(R.id.txtDescrizione)).setText(String.valueOf(allenamento.getDescrizione()));
		((TextView)findViewById(R.id.txtNote)).setText(String.valueOf(allenamento.getNote()));
	
		
		
		int coldis = 0;
		
		switch(allenamento.getIdDiscipline())	
		{
			case 0:
				coldis = getResources().getColor(R.color.color_nuoto);
				break;
			case 1:
				coldis = getResources().getColor(R.color.color_bici);
			break;
			case 2:
				coldis = getResources().getColor(R.color.color_corsa);
			break;
		}
		
		((LinearLayout)findViewById(R.id.llPasso)).setBackgroundColor(coldis);
		((LinearLayout)findViewById(R.id.llVelocita)).setBackgroundColor(coldis);
		((LinearLayout)findViewById(R.id.llBattiti)).setBackgroundColor(coldis);
		((LinearLayout)findViewById(R.id.llCalorie)).setBackgroundColor(coldis);
		((LinearLayout)findViewById(R.id.llDesc)).setBackgroundColor(coldis);
		((LinearLayout)findViewById(R.id.llNote)).setBackgroundColor(coldis);
		
		
		((TextView)findViewById(R.id.txtTempo)).setBackgroundColor(coldis);
		((TextView)findViewById(R.id.txtDist)).setBackgroundColor(coldis);
	
		//int esito = allenamento.getEsito();
		/*
		if (esito ==1)
			((RadioButton)findViewById(R.id.rbEsitoOk)).setChecked(true);
		if (esito ==2)
			((RadioButton)findViewById(R.id.rbEsitoKo)).setChecked(true);
		*/
	}
	
}
